package edu.cicese.tm;

import com.sun.awt.AWTUtilities;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 25/02/11
 * Time: 01:24 PM
 */
public class DialogSpaceEdition extends JDialog {
	public static boolean spaceEdited = false;

	private FrameSpaceManagement frmSpaceManagement;
	private JButton btnColorSelected;
	private JTextField txtSpaceName;
	private Color[] colors = new Color[]{
			Color.WHITE, Color.BLACK, Color.GRAY, Color.RED,
			Color.ORANGE, Color.YELLOW, new Color(182, 255, 0),
			new Color(76, 255, 0), new Color(0, 255, 144),
			new Color(0, 255, 255), new Color(0, 148, 255),
			new Color(0, 38, 255), new Color(72, 0, 255),
			new Color(178, 0, 255), new Color(255, 0, 255),
			new Color(255, 0, 110)
	};

	DialogSpaceEdition(FrameSpaceManagement owner) {
		super(owner);
		spaceEdited = false;
		frmSpaceManagement = owner;
		AWTUtilities.setWindowOpacity(frmSpaceManagement, 0.75f);

		JPanel pMain = new JPanel();
		pMain.setLayout(null);

		SwingUtil.addLabel(new JLabel(), 5, 5, 60, 20, "Nombre:", pMain, null);

		txtSpaceName = new JTextField();
		txtSpaceName.setFont(new Font("Segoe UI", Font.PLAIN, 12));
		txtSpaceName.setForeground(Color.darkGray);
		txtSpaceName.setBounds(60, 5, 152, 20);

		SwingUtil.addLabel(new JLabel(), 5, 35, 60, 20, "Color:", pMain, null);

		btnColorSelected = new JButton();
		btnColorSelected.setBounds(60, 30, 29, 29);
		btnColorSelected.setBorder(new LineBorder(Color.BLACK));

		JButton[] btnColors = new JButton[16];
		int posY = 15, posX = 92;
		int mod;

		for (int i = 0; i < btnColors.length; i++, posX += 15) {
			final int temp = i;
			mod = i % 8;
			if (mod == 0) {
				posX = 92;
				posY += 15;
			}
			btnColors[i] = new JButton();
			btnColors[i].setSize(14, 14);
			btnColors[i].setBackground(colors[i]);
			btnColors[i].setLocation(posX, posY);
			btnColors[i].addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent e) {
					btnColorSelected.setBackground(colors[temp]);
					frmSpaceManagement.colorSelected = colors[temp];
				}
			});
			pMain.add(btnColors[i]);
		}

		JButton btnAccept = new JButton();
		addButton(btnAccept, 71, 67, 32, 32, "Aceptar", "accept", pMain, null);
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionAccept();
			}
		});

		JButton btnCancel = new JButton();
		addButton(btnCancel, 114, 67, 32, 32, "Cancelar", "cancel", pMain, null);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionCancel();
			}
		});

		pMain.add(txtSpaceName);
		pMain.add(btnColorSelected);

		if (frmSpaceManagement.newSpace) {
			setTitle("Nuevo espacio");
			btnColorSelected.setBackground(new Color(0, 38, 255));
			frmSpaceManagement.colorSelected = new Color(0, 38, 255);
		}
		else {
			setTitle("Editar espacio");
			btnAccept.setLocation(51, 67);
			btnCancel.setLocation(93, 67);
			JButton btnChangeArea = new JButton();
			addButton(btnChangeArea, 135, 67, 32, 32, "Cambiar área", "area", pMain, null);
			btnChangeArea.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					actionChangeArea();
				}
			});
			txtSpaceName.setText(frmSpaceManagement.getSelectedSpace().getSpaceName());
			frmSpaceManagement.colorSelected =
					frmSpaceManagement.mpSpaces.get(frmSpaceManagement.getSelectedSpace().getSpaceID()).getSpaceColor();
			btnColorSelected.setBackground(frmSpaceManagement.colorSelected);
		}

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				closeWindow(false);
			}
		});
		setSize(223, 130);
		setResizable(false);
		getContentPane().add(pMain);
		Rectangle window = getBounds();
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);
		setVisible(true);
	}

	//! Adds a JButton
	private void addButton(JButton button, int x, int y, int width, int height,
	                              String toolTipText, String iconName, JPanel pane, Container container) {
		Icon icon = new ImageIcon(getClass().getClassLoader().getResource("icons/" + iconName + ".png"));
		Icon rIcon = new ImageIcon(getClass().getClassLoader().getResource("icons/" + iconName + "O.png"));
		Icon pIcon = new ImageIcon(getClass().getClassLoader().getResource("icons/" + iconName + "P.png"));
		SwingUtil.addButton(button, x, y, width, height, toolTipText, icon, rIcon, pIcon, pane, container);
	}

	//! Closes the dialog and brings back the owner window
	private void closeWindow(boolean acceptCCancelOrArea) {
		if (acceptCCancelOrArea) {
			setVisible(false);
			dispose();
		}
		frmSpaceManagement.newSpace = false;
		frmSpaceManagement.setEnabled(true);
		frmSpaceManagement.toFront();
		AWTUtilities.setWindowOpacity(frmSpaceManagement, 1f);
	}

	//! Called when the button Accept is clicked.
	private void actionAccept() {
		if (!txtSpaceName.getText().trim().equals("")) {
			frmSpaceManagement.spaceName = txtSpaceName.getText().trim();
			if (frmSpaceManagement.newSpace) {
				frmSpaceManagement.selectingArea = true;
				frmSpaceManagement.updateButtonStates(Utilities.SELECTING_AREA);
			} else {
				spaceEdited = true;
				frmSpaceManagement.editSpace(frmSpaceManagement.getSelectedSpace().getSpaceID(), txtSpaceName.getText(),
						btnColorSelected.getBackground(), null, null);
				frmSpaceManagement.updateSpaceList();
			}
			closeWindow(true);
		}
		else {
			JOptionPane.showMessageDialog(this, "Favor de ingresar un nombre para el espacio.",
					"Campo vacio", JOptionPane.ERROR_MESSAGE, 
					new ImageIcon(getClass().getClassLoader().getResource("icons/error.png")));
		}
	}

	//! Called when the button Cancel is clicked
	private void actionCancel() {
		closeWindow(true);
	}

	//! Called when the button Change Area is clicked
	private void actionChangeArea() {
		if (!txtSpaceName.getText().trim().equals("")) {
			closeWindow(true);

			frmSpaceManagement.selectingArea = true;
			spaceEdited = true;
			frmSpaceManagement.spaceName = txtSpaceName.getText().trim();
			frmSpaceManagement.updateButtonStates(Utilities.SELECTING_AREA);
		}
		else {
			JOptionPane.showMessageDialog(this, "Favor de ingresar un nombre para el espacio.", "Campo vacio",
					JOptionPane.ERROR_MESSAGE,
					new ImageIcon(getClass().getClassLoader().getResource("icons/error.png")));
		}
	}

}
